package com.ziyia.workmanager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.work.BackoffPolicy;
import androidx.work.ListenableWorker;
import androidx.work.OneTimeWorkRequest;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.sql.Time;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



    }

    public void doWorkBtn(View view) {
//        PeriodicWorkRequest re = new PeriodicWorkRequest.Builder(SimpleWorker1.class, 15, TimeUnit.MINUTES).build();
        OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder(SimpleWorker1.class)
                .build();
        WorkManager.getInstance(this).enqueue(oneTimeWorkRequest);
    }

    public void t2(View view) {

        // 一次性的任务
        OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder(SimpleWorker1.class)
                .setInitialDelay(10, TimeUnit.SECONDS)
                .setBackoffCriteria(BackoffPolicy.LINEAR, 10, TimeUnit.MINUTES)
                .build();

        PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(SimpleWorker1.class, 15, TimeUnit.MINUTES)
                .setBackoffCriteria(BackoffPolicy.LINEAR, 10, TimeUnit.MINUTES)
                .build();
        WorkManager.getInstance(this).enqueue(oneTimeWorkRequest);
    }


    public void t3(View view) {
        OneTimeWorkRequest request = new OneTimeWorkRequest.Builder(SimpleWorker1.class)
                .setInitialDelay(10, TimeUnit.SECONDS)
                .build();
        WorkManager.getInstance(this)
                .getWorkInfoByIdLiveData(request.getId())
                .observe(this, new Observer<WorkInfo>() {
                    @Override
                    public void onChanged(WorkInfo workInfo) {
                        String TAG = "任务执行结果";
                        if (workInfo.getState() == WorkInfo.State.SUCCEEDED) {
                            Log.d(TAG, "成功");
                        } else {
                            Log.d(TAG, "失败");
                        }
                    }
                });

        WorkManager.getInstance(this).enqueue(request);
    }

    public void then(View view) {
        // 任务1，5秒后执行 - 只会执行一次
        OneTimeWorkRequest r1 = new OneTimeWorkRequest.Builder(SimpleWorker1.class)
                .setInitialDelay(5, TimeUnit.SECONDS)
                .build();

        // 执行2，10秒后执行 - 只会执行一次
        OneTimeWorkRequest r2 = new OneTimeWorkRequest.Builder(SimpleWorker2.class)
                .setInitialDelay(10, TimeUnit.SECONDS)
                .build();

        // 执行3 - 每15分钟执行一次
        OneTimeWorkRequest r3 = new OneTimeWorkRequest.Builder(SimpleWorker3.class)
                .setInitialDelay(15, TimeUnit.SECONDS)
                .build();

        // 注意：只可以执行一次性的任务。
        WorkManager.getInstance(this)
                .beginWith(r1)
                .then(r2)
                .then(r3)
                .enqueue();

    }
}